package com.vitabuybuy.tv.ui.mine.talent.task.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.FieldLabelDto
import com.vitabuybuy.tv.dto.TaskPoolDto
import com.vitabuybuy.tv.inter.OnTaskItemClickListener
import com.vitabuybuy.tv.network.ApplyTalentRepository
import com.vitabuybuy.tv.network.TaskRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class TaskViewModel : BaseViewModel() {

    val itemOnClickListener = object : OnTaskItemClickListener {
        override fun onItemClick(item: TaskPoolDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(2, obj = item))
        }
    }

    val taskList = ObservableArrayList<TaskPoolDto>()

    val taskBinding: OnItemBind<TaskPoolDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, _: TaskPoolDto ->
            itemBinding.set(BR.itemBean, R.layout.item_task).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    fun getTaskPoolData(pageNumber: Int, params: Map<String, Any>) {
        rxHttpRequest {
            onRequest = {
                val it = TaskRepository.getTaskList(params).await()
                if (pageNumber == 1) {
                    taskList.clear()
                }
                taskList.addAll(it)
                if (taskList.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                } else {
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    fun getTaskManagerList(pageNumber: Int, params: Map<String, Any>) {
        rxHttpRequest {
            onRequest = {
                val it = TaskRepository.getTaskManagerList(params).await()
                if (pageNumber == 1) {
                    taskList.clear()
                }
                val list = ArrayList<TaskPoolDto>()
                for (dto in it) {
                    dto.task.id = dto.id
                    list.add(dto.task)
                }
                taskList.addAll(list)
                if (taskList.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                } else {
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    val labellist = MutableLiveData<MutableList<FieldLabelDto>>()
    fun getFieldLabelList() {
        rxHttpRequest {
            onRequest = {
                labellist.value = ApplyTalentRepository.getFieldLabelList().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
